<template>
  <div class="login_container">
    <!-- 登录框 -->
    <div class="login_box">
      <!-- 图标框 -->
      <div class="avatar_box">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <!-- 输入框 -->
      <el-form
        label-width="0px"
        :model="loginForm"
        class="from_box"
        :rules="rules"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          >
          </el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-goods"
            v-model="loginForm.password"
          >
          </el-input>
        </el-form-item>
        <el-row class="btns">
          <el-button type="primary" round @click="login">登录</el-button>
          <el-button type="info" round @click="clearData">重置</el-button>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
        red: 123,
      },
      rules: {
        account: [
          { required: true, message: "请输入账号名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        passWord: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    clearData() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http({
          method: "post",
          url: "/login",
          data: {
            username: this.loginForm.username,
            password: this.loginForm.password,
          },
        });
        // console.log(res);
        if (res.meta.status !== 200)
          return this.$alert("登录失败，请重试", "登录失败", {
            confirmButtonText: "确定",
            callback: () => {
              this.clearData();
            },
          });
        sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -150px 0 0 -225px;
  border-radius: 3px;

  .avatar_box {
    width: 130px;
    height: 130px;
    padding: 10px;
    position: absolute;
    left: 50%;
    border-radius: 50%;
    border: 1px solid #eee;
    background-color: #fff;
    box-shadow: 0 0 10px #ddd;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.from_box {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
}
.btns {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
</style>
